﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using memoria_partilhada = BD2_5479_5513.SharedMemory;
using dbcontrol = BD2_5479_5513.DatabaseControl;

namespace BD2_5479_5513
{
    public partial class FormInscritosAval : Form
    {

        public int aluno = 0;
        public int cod_avalicacao = 0;
        public double nota = 0;

        public FormInscritosAval()
        {
            InitializeComponent();
            preenchimento_inicial();
        }

        public void preenchimento_inicial()
        {
            memoria_partilhada mem = new memoria_partilhada();
            dbcontrol bd = new dbcontrol();

            this.cod_avalicacao = mem.avaliacao_escolhida;

            List<List<string>> lista_inscritos = bd.getInscritos(this.cod_avalicacao);

            var n_inscritos = lista_inscritos.Count();

            this.Controls.Remove(this.tableLayoutPanel1);

            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();

            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();

            this.tableLayoutPanel1.ColumnCount = 4;
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 15.38461F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 84.61539F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 43F));
            this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 82F));
            this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
            this.tableLayoutPanel1.Controls.Add(this.label2, 1, 0);
            this.tableLayoutPanel1.Controls.Add(this.label3, 2, 0);
            this.tableLayoutPanel1.Controls.Add(this.label4, 3, 0);
            this.tableLayoutPanel1.Location = new System.Drawing.Point(13, 13);
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = 1 + n_inscritos;
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.132421F));
            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 90.86758F));
            this.tableLayoutPanel1.Size = new System.Drawing.Size(484, 311);
            this.tableLayoutPanel1.TabIndex = 15;

            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(3, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(19, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Nº";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(58, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(35, 13);
            this.label2.TabIndex = 1;
            this.label2.Text = "Nome";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(361, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(30, 13);
            this.label3.TabIndex = 2;
            this.label3.Text = "Nota";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(404, 0);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(65, 13);
            this.label4.TabIndex = 3;
            this.label4.Text = "Atribuir Nota";

            int y = 1;

            foreach (var item in lista_inscritos)
            {
                Label label_numero = new System.Windows.Forms.Label();
                label_numero.AutoSize = true;
                label_numero.Text = item[0].ToString();
                this.tableLayoutPanel1.Controls.Add(label_numero, 0, y);

                Label label_nome = new System.Windows.Forms.Label();
                label_nome.AutoSize = true;
                label_nome.Text = item[1].ToString();
                this.tableLayoutPanel1.Controls.Add(label_nome, 1, y);

                TextBox nota = new System.Windows.Forms.TextBox();
                nota.Text = item[2].ToString();
                nota.Width = 25;
                nota.Tag = item[0].ToString();
                nota.TextChanged += new System.EventHandler(this.nota_changed);
                this.tableLayoutPanel1.Controls.Add(nota, 2, y);

                Button ButtonNota = new Button();
                ButtonNota.Text = "Dar Nota";
                ButtonNota.Tag = item[0].ToString();
                ButtonNota.Click += new System.EventHandler(this.ButtonNota_click);
                this.tableLayoutPanel1.Controls.Add(ButtonNota, 3, y);

                y++;
            }

            this.Controls.Add(this.tableLayoutPanel1);
        }

        private void ButtonNota_click(Object sender, EventArgs e)
        {
            dbcontrol bd = new dbcontrol();

            var msg = bd.Atribuir_Nota(this.aluno, this.cod_avalicacao, this.nota);

            //System.Windows.Forms.MessageBox.Show(this.cod_avalicacao.ToString() + " | " + this.aluno.ToString() + " | " + this.nota.ToString());
            System.Windows.Forms.MessageBox.Show(msg);
        }

        private void nota_changed(Object sender, EventArgs e)
        {
            TextBox btn = sender as TextBox;

            if (btn != null)
            {
                this.aluno = Int32.Parse(btn.Tag.ToString());
                this.nota = Double.Parse(btn.Text.ToString());
            }
            else
            {
                this.nota = 0;
                this.aluno = 0;
            }
        }

    }
}
